package com.dong.repeatsubmit.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dong.repeatsubmit.constant.Constant;
import com.dong.repeatsubmit.entity.AppNameConfigDto;
import com.dong.repeatsubmit.entity.AppNameConfigVO;
import com.dong.repeatsubmit.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.regex.Pattern;

/**
 * @auther yadong.xu
 * @date 2024/2/22
 */
@Slf4j
public class  ChangeSkinService implements Callable {

        public static void main(String[] args) {
            System.out.println(isNumeric(35));
            System.out.println(feib(35));
        }

        // 1 1 2 3 5 8 13 21 34 56
    public static int isNumeric(int num) {
        if (num < 3){
            return 1;
        }else {
            return isNumeric(num -1) + isNumeric(num -2);
        }
    }

    // 1 1 2 3 5 8 13 21 34 56
    public static int feib(int n){
        int a = 1;
        int b = 1 ;
        for (int i = 1; i <n-1; i++) {
            int k = a;
            if (n < 3){                    //n ==3   res 2  a = 2 b =1 k =1
                return 1;                   // n ==4  res 3  a =3 b =2  n==5 res 5 a = 5 b = 3 k = 3
            }
            a = a+ b;
            b = k;
        }
        return a;
    }

    public List<AppNameConfigVO> doChangSkin(){
        ExecutorService executorService= Executors.newFixedThreadPool(10);
        String usrlZh= "https://pipeline.ebonex.io/api/cms/publish/raw/content?chunkId=54&env=1";
        long start = System.currentTimeMillis();
        String value = FileUtil.downloader(usrlZh);
        //log.info("get first config 请求执行时长:{}",System.currentTimeMillis() - start);
        Integer env = Integer.parseInt(usrlZh.substring(usrlZh.lastIndexOf("=")+1));
        log.info("当前环境是:{}",env);
        //再根据配置的id获取具体的url
        List<AppNameConfigDto> appNameConfigs = JSONObject.parseArray(value, AppNameConfigDto.class);
        List<AppNameConfigVO> appNameConfigVOS = new ArrayList<>(appNameConfigs.size());
        CountDownLatch latch = new CountDownLatch(3);
        for (AppNameConfigDto appNameConfig : appNameConfigs) {
            AppNameConfigVO appNameConfigVO = AppNameConfigVO.builder().name(appNameConfig.getName())
                    .assertId(appNameConfig.getAssertId())
                    .themeId(appNameConfig.getThemeId())
                    .assertH5Id(appNameConfig.getAssertH5Id())
                    .themeColor(appNameConfig.getThemeColor())
                    .swipUrl(appNameConfig.getSwipUrl())
                    .build();

            getThemeUrl(appNameConfigVO,appNameConfig,env);
            getAssertUrl(appNameConfigVO,appNameConfig,env);
            getAssertH5Url(appNameConfigVO,appNameConfig,env);
            //long start1 = System.currentTimeMillis();
            appNameConfigVOS.add(appNameConfigVO);
        }
        log.info("get first config 请求执行时长:{}",System.currentTimeMillis() - start);
        return appNameConfigVOS;
    }

    @Override
    public List<AppNameConfigVO> call() {
        List<AppNameConfigVO> appNameConfigVOS = doChangSkin();
        return appNameConfigVOS;
    }


    private void getAssertH5Url(AppNameConfigVO appNameConfigVO, AppNameConfigDto appNameConfig, Integer env) {
        String assertUrlConfig = FileUtil.downloader(Constant.APP_URL+appNameConfig.getAssertId());
        String assertUrl = getUrl(env, assertUrlConfig);
        //log.info("assertUrl：{}",assertUrl);
        appNameConfigVO.setAssertUrl(assertUrl);
    }

    private void getAssertUrl(AppNameConfigVO appNameConfigVO, AppNameConfigDto appNameConfig, Integer env) {
        String assertH5Config = FileUtil.downloader(Constant.APP_URL+appNameConfig.getAssertH5Id());
        String assertH5Url = getUrl(env, assertH5Config);
        //log.info("assertH5Url：{}",assertH5Url);
        appNameConfigVO.setAssertUrlH5(assertH5Url);
    }

    private void getThemeUrl(AppNameConfigVO appNameConfigVO, AppNameConfigDto appNameConfig,Integer env) {
        String themeUrlConfig = FileUtil.downloader(Constant.APP_URL+appNameConfig.getThemeId());
        String themeUrl = getUrl(env, themeUrlConfig);
        //log.info("themeUrl：{}",themeUrl);
        appNameConfigVO.setThemeUrl(themeUrl);
    }

    private String getUrl(Integer env, String themeUrlConfig) {
        JSONObject jsonObject = JSONObject.parseObject(themeUrlConfig);
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        for (Object o : jsonArray) {
            JSONObject obj = (JSONObject) o;
            if (obj.getInteger("env") == env){
                return obj.getString("fileUrl");
            }
        }
        return null;
    }
}
